package com.kfm.base.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class Demo05 {

    public static void main(String[] args) {
        Class<Student> studentClass = Student.class;

        try {
            Constructor<Student> declaredConstructor = studentClass.getDeclaredConstructor();
            declaredConstructor.setAccessible(true);
            // 创建对象
            Student student = declaredConstructor.newInstance();

//            // 获取字段
//            Field name = studentClass.getDeclaredField("name");
//            name.setAccessible(true);
//            // 设置字段值
//            name.set(student, "张三");
//
//            System.out.println(student);

            // 获取方法
            Method setName = studentClass.getDeclaredMethod("setName", String.class);
            setName.setAccessible(true);

            // 调用方法
            setName.invoke(student, "李四");

            System.out.println(student);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
